package com.gk.panda.apis.service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.gk.panda.apis.config.TargetedSms;
import com.gk.panda.apis.entity.targetedSms.dto.TargetedSmsDto;
import com.gk.panda.apis.entity.targetedSms.vo.ResultVo;
import com.gk.panda.apis.entity.targetedSms.vo.TargetedSmsVo;
import com.gk.panda.apis.service.TargetedSmsService;
import com.gk.panda.commons.constant.Constants;
import com.gk.panda.commons.exception.ServerException;
import com.gk.panda.commons.util.http.HttpClientResult;
import com.gk.panda.commons.util.http.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName: TargetedSmsServiceImpl
 * @Description: 靶向短信接入
 * @author: Sir.yang
 * @date: 2022/11/7 10:45
 * @version: 1.0
 **/
@Service
@Slf4j
public class TargetedSmsServiceImpl implements TargetedSmsService {

    @Autowired
    private TargetedSms targetedSms;


    /**
     * @description: 获取登录跳转地址
     * @param: []
     * @return: java.lang.String
     * @date: 2022/11/7 15:25
     * @version: 1.0
     **/
    @Override
    public String getLoginUrl(){
        String salt = UUID.randomUUID().toString().replaceAll(Constants.HORIZONTAL_LINE,"");
        //获取token
        StringBuffer str = new StringBuffer();
        str.append(targetedSms.getAccount());
        str.append(salt);
        str.append(targetedSms.getSecretKey());
        String token = DigestUtils.md5Hex(str.toString());
        //获取登录地址
        StringBuffer loginUrl = new StringBuffer();
        loginUrl.append(targetedSms.getLoginUrl());
        loginUrl.append("?");
        loginUrl.append("account=");
        loginUrl.append(targetedSms.getAccount());
        loginUrl.append("&salt=");
        loginUrl.append(salt);
        loginUrl.append("&token=");
        loginUrl.append(token);
        return loginUrl.toString();
    }


    /**
     * @description: 调用靶向短信接口获取数据
     * @param: [startDate：发送记录查询起始时间，格式为：2022-01-01，默认为2022-01-01 00:00:00]
     * @param: [endDate：发送记录查询截至时间，格式为：2022-01-10，默认为2022-01-10 23:59:59]
     * @param: [id：该任务记录id，ID可分为一般和核心区域]
     * @return: java.util.List<com.gk.panda.apis.entity.TargetedSms.vo.TargetedSmsVo>
     * @date: 2022/11/7 16:03
     * @version: 1.0
     **/
    @Override
    public List<TargetedSmsVo> getetTargetedSmsData(String startDate, String endDate, Integer id){
        if(StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate) || null == id){
            throw new ServerException("调用靶向短信接口请求参数不能为空！");
        }
        TargetedSmsDto dto = new TargetedSmsDto();
        dto.setStart(startDate);
        dto.setEnd(endDate);
        dto.setAccount(targetedSms.getAccount());
        dto.setId(id);
        String salt = UUID.randomUUID().toString().replaceAll(Constants.HORIZONTAL_LINE,"");
        dto.setSalt(salt);
        //token=account+end+id+salt+secretKey+start
        StringBuffer path = new StringBuffer();
        path.append(dto.getAccount());
        path.append(dto.getEnd());
        path.append(id);
        path.append(dto.getSalt());
        path.append(targetedSms.getSecretKey());
        path.append(dto.getStart());
        String token = DigestUtils.md5Hex(path.toString());
        dto.setToken(token);
        HttpClientResult httpClientResult = null;
        try {
            httpClientResult = HttpClientUtils.doPostJsonForOtherInterface(targetedSms.getRequestUrl(), JSONUtil.toJsonStr(dto), null);
        } catch (Exception e) {
            log.error("调用靶向短信接口失败：{}", e.getMessage());
            throw new ServerException("调用靶向短信接口失败");
        }
        //解析
        if (Constants.SUCCESS != httpClientResult.getCode()) {
            throw new ServerException("调用靶向短信接口失败!");
        }
        log.info("返回值为：{}", httpClientResult.getContent());
        ResultVo resultVo = JSON.parseObject(httpClientResult.getContent(), ResultVo.class);
        if (!Constants.LOGIN_SUCCESS_STATUS.equals(resultVo.getResult())) {
            throw new ServerException(resultVo.getMessage());
        }
        List<TargetedSmsVo> targetedSmsVoList = new ArrayList<>();
        if (StringUtils.isNotBlank(resultVo.getData())) {
            targetedSmsVoList = JSON.parseArray(resultVo.getData(), TargetedSmsVo.class);
        }
        return targetedSmsVoList;
    }
}
